package com.dts.yjnb.service.admincontroller;
import com.dts.common.core.domain.AjaxResult;
import com.dts.common.core.page.TableDataInfo;
import com.dts.yjnb.service.domain.Market;
import com.dts.yjnb.service.domainDto.MarketDTO;
import com.dts.yjnb.service.service.IMarketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.dts.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 农贸市场管理 信息操作处理
 *
 * @author dts
 */
@Api(tags = "农贸市场管理相关接口")
@RestController
@RequestMapping("/yjnb/market")
@Slf4j
public class MarketController extends BaseController
{

    @Autowired
    private IMarketService marketService;

    /**
     * 农贸市场分页查询（支持模糊搜索和地区筛选）
     */
    @GetMapping("/page")
    @ApiOperation("农贸市场分页查询")
    @Cacheable(cacheNames = "marketPage", key = "#marketDTO.page + '-' + #marketDTO.pageSize + '-' + #marketDTO.marketName + '-' + #marketDTO.region + '-' + #marketDTO.beginTime + '-' + #marketDTO.endTime")
    public TableDataInfo page(MarketDTO marketDTO) {
        log.info("农贸市场分页查询:{}", marketDTO);
        TableDataInfo tableDataInfo = marketService.pageQuery(marketDTO);
        return tableDataInfo;
    }

    /**
     * 根据ID查询农贸市场详情
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询农贸市场详情")
    public AjaxResult getById(@PathVariable Long id) {
        log.info("根据ID查询农贸市场详情:{}", id);
        Market market = marketService.getByid(id);
        return AjaxResult.success(market);
    }



    /**
     * 新增农贸市场
     */
    @PostMapping("/add")
    @ApiOperation("新增农贸市场")
    @CacheEvict(cacheNames = "marketPage", allEntries = true)
    public AjaxResult add(@Valid @RequestBody MarketDTO marketDTO) {
        log.info("新增农贸市场:{}", marketDTO);
        marketService.add(marketDTO);
        return AjaxResult.success("新增成功");
    }

    /**
     * 批量删除农贸市场
     */
    @DeleteMapping("/deletes")
    @ApiOperation("市场批量删除")
    @CacheEvict(cacheNames = "marketPage", allEntries = true)
    public AjaxResult deleteByIds(@RequestParam List<Long> ids) {
        log.info("市场批量删除:{}", ids);
        marketService.deleteByIds(ids);
        return AjaxResult.success("删除成功");
    }

    /**
     * 更新农贸市场信息
     */
    @PutMapping("/update")
    @ApiOperation("农贸市场信息编辑")
    @CacheEvict(cacheNames = "marketPage", allEntries = true)
    public AjaxResult update(@Valid @RequestBody MarketDTO marketDTO) {
        log.info("农贸市场信息编辑:{}", marketDTO);
        marketService.update(marketDTO);
        return AjaxResult.success("编辑成功");
    }
}

